<?php 
declare (strict_types = 1);
namespace app\api\controller;
use app\common\controller\Api;
use think\Request;
class BusinessHostScript extends Api
{
    protected $with = ['script'=>['business','script_screen','script_setting_nd','script_setting_xs','script_class.script_setting','script_role']];
    //商户端-DM管理-擅长剧本列表
    public function index()
    {
        $this->businessCheckLogin();
        $post = param();
        if(!isset($post['id'])) return msgerror('参数缺失');
        $limit = input('limit') ?: 20;
        $data = $this->model->where([['business_host_id','=',$post['id']]])->with($this->with)->order('id desc')->paginate($limit);
        return datasuccess($data);
    }
    //商户端-DM管理-擅长剧本-添加
    public function add()
    {
        $this->businessCheckLogin();
        $post = param();
        if(!empty($post)){
            if(!isset($post['ids']) || !isset($post['id'])) return msgerror('参数缺失');
            $ids = explode(',',$post['ids']);
            $ids = array_filter($ids);
            $arr = [];
            foreach ($ids as $v)
            {
                $data = $this->model->where([
                    ['business_host_id','=',$post['id']],
                    ['script_id','=',$v]
                ])->find();
                if(!empty($data) && !empty($data->toArray())){
                    continue;
                }
                $arr[] = ['business_host_id'=>$post['id'],'script_id'=>$v];
            }
            if(!empty($arr)){
                $this->model->saveAll($arr);
            }else{
                return msgerror('添加的擅长剧本全部以存在，不可重复添加，添加失败');
            }
            return datasuccess();
        }else{
            return msgerror('参数缺失');
        }
    }
}